﻿Shader "Unlit/Terrain"
{
    Properties
    {
        [NoScaleOffset] _MainTex ("色阶图", 2D) = "white" {}
        _MainTex_Tiling ("主 Tiling", Vector) = (1,1,0,0)

        [NoScaleOffset] _RenTex ("红", 2D) = "white" {}
        _RenTex_Tiling("红 Tiling", Vector) = (50,50,0,0)
        
        [NoScaleOffset] _BlueTex ("蓝", 2D) = "white" {}
        _BlueTex_Tiling("蓝 Tiling", Vector) = (50,50,0,0)
        
        [NoScaleOffset] _GreenTex ("绿", 2D) = "white" {}
        _GreenTex_Tiling("绿 Tiling", Vector) = (50,50,0,0)

        _Diffuse("Diffuse",Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { 
            "RenderType"="Opaque"
            "LightMode" = "ForwardBase"
        }
        LOD 200

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float3 uvMain : TEXCOORD0;
                float2 uvImg : TEXCOORD1;
                float2 uvRed : TEXCOORD2;
                float2 uvBlue : TEXCOORD3;
                float2 uvGreen : TEXCOORD4;
                UNITY_FOG_COORDS(4)
                float4 vertex : SV_POSITION;
            };

            fixed4 _Diffuse;
            sampler2D _MainTex;
            fixed4 _MainTex_TexelSize;
            fixed4 _MainTex_Tiling;

            float4 _MainTex_ST;

            sampler2D _RenTex;
            float4 _RenTex_Tiling;
            
            sampler2D _BlueTex;
            float4 _BlueTex_Tiling;
            
            sampler2D _GreenTex;
            float4 _GreenTex_Tiling;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uvMain = UnityObjectToWorldNormal(v.normal);
                
                o.uvImg = v.uv *  _MainTex_Tiling.xy;
                o.uvRed = v.uv * _RenTex_Tiling.xy;
                o.uvBlue = v.uv * _BlueTex_Tiling.xy;
                o.uvGreen = v.uv * _GreenTex_Tiling.xy;
                
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed3 uvMain = normalize(i.uvMain);
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(uvMain,worldLight));
                fixed3 color = ambient+diffuse;
                
                float2 uvOffset = _MainTex_TexelSize.xy * 2;
                fixed4 col = (tex2D(_MainTex, i.uvImg) + 
                            tex2D(_MainTex, i.uvImg - uvOffset) + 
                            tex2D(_MainTex, i.uvImg + uvOffset)) / 3;

                float3 weights = col.rgb * col.rgb;
                weights /= (weights.r + weights.g + weights.b + 0.001);

                fixed4 redCol = tex2D(_RenTex, i.uvRed);
                fixed4 greenCol = tex2D(_GreenTex, i.uvGreen);
                fixed4 blueCol = tex2D(_BlueTex, i.uvBlue);
                
                fixed4 finalColor = redCol * weights.r + 
                                greenCol * weights.g + 
                                blueCol * weights.b;

                float luminance = Luminance(finalColor.rgb);
                float blendFactor = smoothstep(0.2, 0.3, luminance);
                finalColor = lerp(blueCol, finalColor, blendFactor);
                
                finalColor.rgb *= color;

                UNITY_APPLY_FOG(i.fogCoord, finalColor);
                return finalColor;
            }
            ENDCG
        }
    }
}